﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;
using Panny.Data;
using System.Data.SqlClient;
namespace Easydp.Models
{
    public class TrainStationsManger
    {
        public static string connectionString = ConfigurationSettings.AppSettings["ConnectionString"];

        /// <summary>
        /// 更新一条数据
        /// </summary>
        /// 
        public int Update(Entities.TT_TrainRoute model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update TrainStations set ");
            strSql.Append("TrainNumber=@TrainNumber,");
            strSql.Append("Station=@Station,");
            strSql.Append("Mileage=@Mileage,");
            strSql.Append("StartTime=@StartTime,");
            strSql.Append("EndTime=@EndTime,");
            strSql.Append("StopTime=@StopTime,");
            strSql.Append("RunTime=@RunTime,");
            strSql.Append("m_yz=@m_yz,");
            strSql.Append("m_rz=@m_rz,");
            strSql.Append("m_yw=@m_yw,");
            strSql.Append("m_rw=@m_rw,");
            strSql.Append("Position=@Position,");
            strSql.Append("ShortCode=@ShortCode");
            strSql.Append(" where LineID=@LineID ");
             using (SqlText updation = new SqlText(strSql.ToString()))
            {
                updation.AddParameter("@TrainNumber", model.TrainNumber);
                updation.AddParameter("@Station", model.Station);
                updation.AddParameter("@Mileage", model.Mileage);
                updation.AddParameter("@StartTime", model.StartTime);
                updation.AddParameter("@EndTime", model.EndTime);
                updation.AddParameter("@StopTime", model.StopTime);
                updation.AddParameter("@RunTime", model.RunTime);
                updation.AddParameter("@m_yz", model.M_yz);
                updation.AddParameter("@m_rz", model.M_rz);
                updation.AddParameter("@m_yw", model.M_yw);
                updation.AddParameter("@m_rw", model.M_rw);
                updation.AddParameter("@Position", model.Position);
                updation.AddParameter("@ShortCode", model.ShortCode);
                return updation.ExecuteNonQuery();

            }

           
        }




        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Entities.TT_TrainRoute GetModel(int LineID)
        {
            Entities.TT_TrainRoute model = new Easydp.Models.Entities.TT_TrainRoute();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 LineID,TrainNumber,Station,Mileage,StartTime,EndTime,StopTime,RunTime,m_yz,m_rz,m_yw,m_rw,Position,ShortCode from TrainStations ");
            strSql.Append(" where LineID=@LineID ");
            using (SqlText selection = new SqlText(strSql.ToString()))
            {
                selection.AddParameter("@LineID", LineID);

                while (selection.Read())
                {
                    if (selection["LineID"].ToString() != "")
                    {
                       model.LineID = int.Parse(selection["LineID"].ToString());
                    }
                    model.TrainNumber = selection["TrainNumber"].ToString();
                    model.Station = selection["Station"].ToString();
                        model.Mileage =selection["Mileage"].ToString();
           
                    model.StartTime = selection["StartTime"].ToString();
                    model.EndTime = selection["EndTime"].ToString();
                    model.StopTime = selection["StopTime"].ToString();
                    model.RunTime = selection["RunTime"].ToString();
                    model.M_yz = selection["m_yz"].ToString();
                    model.M_rz = selection["m_rz"].ToString();
                    model.M_yw = selection["m_yw"].ToString();
                    model.M_rw = selection["m_rw"].ToString();
   
                        model.Position = selection["Position"].ToString();
                  
                    model.ShortCode = selection["ShortCode"].ToString();
                  
                }
            }
            return model;
           
        }


        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetDadaset(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select *");
            strSql.Append(" FROM TrainStations ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }

            using (SqlConnection sqlcon = new SqlConnection(connectionString))
            {
                DataSet ds = new DataSet();
                try
                {
                    sqlcon.Open();
                    SqlDataAdapter command = new SqlDataAdapter(strSql.ToString(), connectionString);
                    command.Fill(ds, "ds");
                }
                catch (System.Data.SqlClient.SqlException ex)
                {
                    throw new Exception(ex.Message);
                }
                finally
                {
                    sqlcon.Close();
                }
                return ds;
            }

        }


        /// <summary>
        /// 得到列表
        /// </summary>
        public List<Entities.TT_TrainRoute> GetList(int LineID)
        {
            List<Entities.TT_TrainRoute> list = new List<Easydp.Models.Entities.TT_TrainRoute>();
            using (SqlText selectlist = new SqlText("select * from TrainStations where LineID=@LineID"))
            {
                selectlist.AddParameter("@LineID", LineID);
                while (selectlist.Read())
                {
                    Entities.TT_TrainRoute model = new Easydp.Models.Entities.TT_TrainRoute();
                    if (selectlist["LineID"].ToString() != "")
                    {
                        model.LineID = int.Parse(selectlist["LineID"].ToString());
                    }
                    model.TrainNumber = selectlist["TrainNumber"].ToString();
                    model.Station = selectlist["Station"].ToString();
                    model.Mileage =selectlist["Mileage"].ToString();
                    
                    model.StartTime = selectlist["StartTime"].ToString();
                    model.EndTime = selectlist["EndTime"].ToString();
                    model.StopTime = selectlist["StopTime"].ToString();
                    model.RunTime = selectlist["RunTime"].ToString();
                    model.M_yz = selectlist["m_yz"].ToString();
                    model.M_rz = selectlist["m_rz"].ToString();
                    model.M_yw = selectlist["m_yw"].ToString();
                    model.M_rw = selectlist["m_rw"].ToString();
      
                    model.Position =selectlist["Position"].ToString();
                    
                    model.ShortCode = selectlist["ShortCode"].ToString();
                    list.Add(model);
                }
            }
            return list;
        }

    }
}
